//
//  LineDetailViewController.m
//  Rail
//
//  Created by hychen on 2018/3/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "LineDetailViewController.h"
#import "SiteDetailViewController.h"
#import "LineSectionDetailViewController.h"

#import "LineDetailLongDescTableViewCell.h"
#import "LineDetailDescTableViewCell.h"

#import "WarnLevelConfig.h"
@interface LineDetailViewController ()
@property (nonatomic, strong) UIButton *collectionButton;
@property (nonatomic, strong) LineDetailModel *detailModel;
@property (nonatomic, strong) LinePointDetailView *headerView;
@property (nonatomic, assign) BOOL isLoading;
@end

@implementation LineDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"线路详情"];
    
    self.collectionButton = [self.navigationItem addRightNavItem:self normalImage:[UIImage imageNamed:@"app_title_bar_back_n_ic"] highlightedImage:[UIImage imageNamed:@"app_title_bar_back_p_ic"] selector:@selector(clickCollectionButton:)];
    [self.collectionButton setImage:[UIImage imageNamed:@"app_title_bar_back_n_ic"] forState:UIControlStateNormal];
    [self.collectionButton setImage:[UIImage imageNamed:@"app_title_bar_back_p_ic"] forState:UIControlStateSelected];
    
//    [self.tableView registerClass:[LineDetailLongDescTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LineDetailLongDescTableViewCell class])];
//    [self.tableView registerClass:[LineDetailDescTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LineDetailDescTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LineDetailLongDescTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LineDetailLongDescTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LineDetailDescTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LineDetailDescTableViewCell class])];
    
    [self setupTableViewData];
    
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickCollectionButton:(UIButton *)button {
    BOOL isFav = button.selected;
    if (isFav) {
        [self cancelFactoryCollection];
    }
    else {
        [self addFactoryCollection];
    }
}

- (void)renderHeaderView {
    BMWeakSelf
    UIColor *lineColor = [BMUtils colorWithString:self.detailModel.line_color];
    LinePointDetailView *headerView = [[LinePointDetailView alloc] initWithLineColor:lineColor];
    [headerView setBlockClickButton:^(id obj) {
        if ([obj isKindOfClass:[LineStationModel class]]) {
            //点击车站
            [weakSelf gotoStationDetail:obj];
        }
        else if ([obj isKindOfClass:[LineSectionModel class]]) {
            //点击标段
            [weakSelf gotoSectionDetail:obj];
        }
    }];
    headerView.lineDetailModel = self.detailModel;
    self.headerView = headerView;
    self.tableView.tableHeaderView = headerView;
}

#pragma mark - 网络请求
- (void)refreshData {
    BMWeakSelf
    [HttpRequest loadLineDetailDataWithLineId:self.lineId success:^(LineDetailModel *object) {
        weakSelf.collectionButton.selected = object.favid > 0;
        weakSelf.detailModel = object;
        [weakSelf renderHeaderView];
        [weakSelf setupTableViewData];
        [weakSelf.tableView reloadData];
        
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];

}

- (void)setupTableViewData {
    
    self.dataArray = @[
                       @{@"title":@"线路名称",@"data":self.detailModel.line_name?:@""},
                       @{@"title":@"线路编号",@"data":self.detailModel.lineno?:@""},
                       @{@"title":@"线路状态",@"data":[WarnLevelConfig lineWorkStatusString:self.detailModel.status]?:@""},
                       @{@"title":@"所属城市",@"data":self.detailModel.city?:@""},
                       @{@"title":@"操作人员",@"data":self.detailModel.op_name?:@""},
                       @{@"title":@"操作时间",@"data":[BMUtils chineseYYYYMMDDHHmmWithServiceTime:self.detailModel.op_time]?:@""},
                       @{@"title":@"线路说明",@"data":self.detailModel.note?:@""},
                       ].mutableCopy;
}

#pragma mark - 收藏
- (void)addFactoryCollection {
    if (self.isLoading) {
        return;
    }
    if (self.detailModel.favid != 0) {
        return;
    }
    self.isLoading = YES;
    Show_Msg(@"收藏中")
    BMWeakSelf
    [HttpRequest addFavWithLineId:self.detailModel.lineid?:@"" areaId:@"" deviceId:@"" success:^(id object) {
        weakSelf.collectionButton.selected = YES;
        weakSelf.detailModel.favid = [object integerValue];
        Show_Msg_Success(@"收藏成功")
        weakSelf.isLoading = NO;
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        weakSelf.isLoading = NO;
    }];
}

- (void)cancelFactoryCollection {
    if (self.isLoading) {
        return;
    }
    self.isLoading = YES;
    Show_Msg(@"取消收藏中")
    BMWeakSelf
    [HttpRequest cancelFavWithFavId:[@(self.detailModel.favid) stringValue]?:@"" success:^(id object) {
        weakSelf.detailModel.favid = 0;
        weakSelf.collectionButton.selected = NO;
        Show_Msg_Success(@"取消收藏成功")
        weakSelf.isLoading = NO;
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        weakSelf.isLoading = NO;
    }];
}

#pragma mark - 跳转
- (void)gotoSectionDetail:(LineSectionModel *)model {
    LineSectionDetailViewController *vc = [LineSectionDetailViewController new];
    vc.sectionId = model.sectionid;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoStationDetail:(LineStationModel *)model {
    SiteDetailViewController *vc = [SiteDetailViewController new];
    vc.stationId = model.stationid;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.dataArray.count - 1) {
        NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
        LineDetailDescTableViewCell *cell = [LineDetailDescTableViewCell cellWithTableView:tableView class:[LineDetailDescTableViewCell class]];
        cell.titleLabel.text = [dict objectForKey:@"title"];
        cell.contentLabel.text = [dict objectForKey:@"data"];
        if ([cell.titleLabel.text isEqualToString:@"线路状态"]) {
            switch (self.detailModel.status) {
                case LineWorkingStatusWorking:
                {
                    cell.contentLabel.textColor = Color_LineWorking;
                    break;
                }
                case LineWorkingStatusFinish:
                {
                    cell.contentLabel.textColor = Color_LineFinish;
                    break;
                }
                case LineWorkingStatusTodo:
                {
                    cell.contentLabel.textColor = Color_LineTodo;
                    break;
                }
                default:
                    cell.contentLabel.textColor = Color_RGB(86, 86, 86);
                    break;
            }
        }
        else {
            cell.contentLabel.textColor = Color_RGB(86, 86, 86);
        }
        return cell;
    }
    else if (indexPath.row < self.dataArray.count) {
        NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
        LineDetailLongDescTableViewCell *cell = [LineDetailLongDescTableViewCell cellWithTableView:tableView class:[LineDetailLongDescTableViewCell class]];
        cell.title = [dict objectForKey:@"title"];
        cell.content = [dict objectForKey:@"data"];
        return cell;
    }
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.dataArray.count - 1) {
        return 50.f;
    }
    else if (indexPath.row < self.dataArray.count) {
        CGFloat height = [tableView fd_heightForCellWithIdentifier:NSStringFromClass([LineDetailLongDescTableViewCell class]) configuration:^(LineDetailLongDescTableViewCell *cell) {
            NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
            cell.title = [dict objectForKey:@"title"];
            cell.content = [dict objectForKey:@"data"];
        }];
        return height;
    }
    return 0;
}

@end
